Parallelizes Data.content_hash() for large datasets#86
Parallelizes Data.content_hash() for large datasets#86JyotinderSingh wants to merge 2 commits intomainfrom
Data.content_hash() for large datasets#86Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the performance of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a well-designed parallel hashing algorithm to improve performance for large datasets, which is a great enhancement. The use of a two-level hash with a ThreadPoolExecutor is a solid approach for this I/O-bound task. The code is clear, and the addition of tests for determinism and boundary conditions is excellent. I have one minor suggestion to make the code slightly more concise and Pythonic.
keras_remote/data/data.py
Outdated
| h.update(relpath.encode("utf-8")) | ||
| h.update(b"\0") | ||
| with open(fpath, "rb") as f: | ||
| for chunk in iter(partial(f.read, 65536), b""): |
There was a problem hiding this comment.
Can you leave a comment as to why it's chunked this way
There was a problem hiding this comment.
I just aligned with the default chunk size for python stdlib utilities. 64-256kb is also the standard nvme page size.
shutil.copyfileobj uses a 64kb chunk, while hashlib.file_digest uses a 256kb chunk
I just added this as a basic optimization to allow us to hash larger datasets more efficiently. I've added some benchmark numbers to the PR description for the same. |
140cb03 to
97b3eb5
Compare
divyashreepathihalli
left a comment
There was a problem hiding this comment.
Thanks for the PR. Just one comment
| def _content_hash_dir(self) -> str: | ||
| # Enumerate all files. Walk in filesystem order (better disk | ||
| # locality) and sort once at the end for determinism. | ||
| file_list = [] |
There was a problem hiding this comment.
Building a full file_list array covering every file in memory before chunking triggers the same RAM-saturation pattern found in other parts of the client/runner. For massive datasets (e.g., 20M+ files), iterating over the structure can cause an Out-Of-Memory (OOM) crash before the pool map even launches.
Problem
Data.content_hash()hashes all file contents sequentially in a single thread. For large datasets (e.g. 20 GB across hundreds of thousands of files), this becomes the bottleneck before GCS upload as the method spends most of its timeblocked on I/O reads that could be issued concurrently.
Solution
This change introduces a two-level parallel hashing algorithm:
os.walkto collect all file paths, sorted once at the end for determinism.SHA-256(relpath + \0 + contents)) via aThreadPoolExecutor. Work is submitted in batches of 512 to avoid creating oneFutureper file (which would mean 1MFutureobjects for a directory with a million files).SHA-256("dir:" + digest_1 + digest_2 + ...).The GIL is released during
open(),read(), andhashlib.update()(for inputs > 2048 bytes), so threads achieve real I/O parallelism and saturate SSD/NVMe queue depth across multiple NAND channels.Design decisions
ThreadPoolExecutorovermultiprocessingFuturecount from 1M to ~2K for million-file datasets.min(32, cpu_count + 4)Breaking change
This changes the hash algorithm from single-pass incremental to two-level, so existing hash values will differ. This is acceptable.
Benchmarking
I used a one-off benchmarking script to understand the performance characteristics of the multi-threaded approach. The following numbers were captured on an 2021 Macbook Pro with an M1 Pro processor.